#!/bin/bash


get_timestamp(){
  date '+%Y-%m-%d %H:%M:%S'
}

log(){
  echo "$(get_timestamp) ${*}"
}

get_env() {
  log "BEGIN get_env ..."
  local pod=$1
  local env_file=$2/$pod
  local ns=${3:-$NAMESPACE}
  local pattern=${4:-"Dockerfile-.*logging*"}
  log ---- Env for $pod
  containers=$(oc -n $ns get po $pod -o jsonpath='{.spec.containers[*].name}')
  for container in $containers
  do
    log ----- Inspecting container $container
    dockerfile=$(oc -n $ns exec $pod -c $container -- ls /root/buildinfo | grep $pattern)||
    if [ -n "$dockerfile" ]
    then
      log ----- Getting buildInfo
      echo Image info: $dockerfile > $env_file
      oc -n $ns exec $pod -c $container -- grep -o "\"build-date\"=\"[^[:blank:]]*\"" /root/buildinfo/$dockerfile >> $env_file || log "---- Unable to get build date"
    fi
    log ----- Getting environement variables
    echo -- Environment Variables >> $env_file
    oc -n $ns exec $pod -c $container -- env | sort >> $env_file
  done
  log "END get_env ..."
}
